<?php
/************************/
# 文件名：connect.php
# 作用：数据库操作类
# 
# 
# 修改时间：2007-6-18
/***********************/

if (!defined('IN_SITE')){ die('Hacking attempt!'); } 

###------------------------ 一般设置 ---------------------------###
class DbConn{
	public $mHost;					// hostname或服务器ip
	public $mUser;					// 登录数据库的用户名
	public $mPassword;				// 数据库密码
	public $mName;					// 数据库名
	public $mConnId;
	public $mQueryId;
	public $Auto_Free = true;
	## 连接数据
	function Dbconn($host,$user,$password,$table){
		$this->mHost = $host;
		$this->mUser = $user;
		$this->mPassword = $password;
		$this->mName = $table;
		$this->mConnId = @mysql_pconnect($this->mHost,$this->mUser,$this->mPassword);
		@mysql_select_db($this->mName);
	}
	## 执行mysql语句
	function Query($Sql){
		$this->mQueryId = @mysql_query($Sql);
		return $this->mQueryId;
	}
	function next_record($id='',$result_type = MYSQL_ASSOC) {
	    if ($id){
	    	return @mysql_fetch_array($id,$result_type);
	    }elseif ($this->mQueryId) {
	    	return @mysql_fetch_array($this->mQueryId,$result_type);
	    }else {
	    	return false;
	    }
	}
	function Query_delete($Sql){
		@mysql_query($Sql);
		return mysql_affected_rows();
	}
	/*
	 * 功能：返回INSERT，UPDATE 或 DELETE 查询 影响的条数
	*/
	function returnAffected(){
		$num = @mysql_affected_rows();
		if ($num > 0){
			return $num;
		}else {
			return false;
		}
	}
	// 添加数据
	function Query_adate($a_data,$mname){
		$i=1;
		foreach($a_data as $key =>$value){
			if($i==1){
				$a_date_key = $key;
				$a_date_value = "'".$value."'";
			}else{
				$a_date_key .= ",".$key;
				$a_date_value .= ",'".$value."'";
			}
			$i++;
		}
		$sql = "insert into ".$mname." (".$a_date_key.") values (".$a_date_value.")";
		$this->Query($sql);
		return $this->returnAffected();
	}
	// 更新数据
	function Query_update($up_data,$up_mmane,$up_id){
		$i=1;
		$j=1;
		//分析数组结构
		if (is_array($up_id)){
			foreach($up_id as $key => $value){
				//unset($up_data[$key]);
				if($j==1){
					$up_limit = ' WHERE '.$key."='".$value."'";
				}else{
					$up_limit .= " AND ".$key."='".$value."'";
				}
				$j++;
			}
		}else {
			$up_limit = $up_id;
		}
		//分析数组结构
		foreach($up_data as $key =>$value){
			if($i==1){
				$up_date = $key."='".$value."'";
			}else{
				$up_date .= ",".$key."='".$value."'";
			}
			$i++;
		}
		$sql = "UPDATE ".$up_mmane." SET ".$up_date.' '.$up_limit;
		return mysql_query($sql);
	}
	## 返回字段的数目(很少用到)
	function Fieldscount($Query){
		$this->mQueryId = $this->Query($Query);
		return @mysql_num_fields($this->mQueryId);
	}
	## 返回符合条件记录的条数
	function Recordcount($Query){
		$this->mQueryId = $this->Query($Query);
		return @mysql_num_rows($this->mQueryId);
	}
	
	## 返回二维数组类型的数据
	function Fetcharray($Query,$result_type = MYSQL_ASSOC){
		$this->mQueryId = $this->Query($Query);
		while($row = @mysql_fetch_array($this->mQueryId,$result_type)){
			$array[] = $row;
		}
		if ($this->Auto_Free){
			$this->freeQueryId();
		}
		return $array;
	}
	//返回分类，制作列表菜单
	function Fetcharray_area($Query,$id='id',$name='name'){
		$this->mQueryId = $this->Query($Query);
		$array_area = array();
		while($row = @mysql_fetch_array($this->mQueryId,MYSQL_ASSOC)){
			$array_area[$row[$id]] = $row[$name];
		}
		if ($this->Auto_Free){
			$this->freeQueryId();
		}
		return $array_area;
	}
	## 返回数组类型的数据，并且带需要
	function Fetcharraynum($Query,$i=0,$result_type = MYSQL_ASSOC){
		$this->mQueryId = $this->Query($Query);
		$i += 0;
		while($row = @mysql_fetch_array($this->mQueryId,$result_type)){
			$i++;
			$row[num] = $i;
			$array[] = $row;
		}
		if ($this->Auto_Free){
			$this->freeQueryId();
		}
		return $array;
	}
	function GetOneLineField($Query,$result_type = MYSQL_NUM){
		$array = array();
		$this->mQueryId = $this->Query($Query);
		while($row = @mysql_fetch_array($this->mQueryId,$result_type)){
			$array[] = $row[0];
		}
		if ($this->Auto_Free){
			$this->freeQueryId();
		}
		return $array;
	}
	#####  得到一个字段的数据
	function Getonefield($Query,$arraykey='',$result_type = MYSQL_ASSOC){
		$this->mQueryId = $this->Query($Query);
		$array = @mysql_fetch_array($this->mQueryId,$result_type);
		if (is_array($array)){
			if ($arraykey){
				return $array[$arraykey];
			}else {
				foreach ($array as $key=>$value){
					return $array[$key];
				}
			}
		}
	}
	/////返回一维数组
	function Getonerecord($Query,$result_type = MYSQL_ASSOC){
		$this->mQueryId	= $this->Query($Query);
		$array = @mysql_fetch_array($this->mQueryId,$result_type);
		if ($this->Auto_Free){
			$this->freeQueryId();
		}
		return $array;
	}
	#### 取插入数据时最后一个数据，即当前插入的数据
	function Insertid(){
		$id = @mysql_insert_id();
		return $id;
	}
	#### 初始指针 public: discard the query result 
  	function freeQueryId($Queryid='') {
		if($Queryid){
			@mysql_free_result($Queryid);
		}
      	@mysql_free_result($this->mQueryId);
      	$this->mQueryId = 0;
  	}
	#### 关闭数据库
	function Dbclose(){
		if($this->mConnId){
			if($this->mQueryId)
				@mysql_free_result($this->mQueryId);
			$result = @mysql_close($this->mConnId);
			return $result;
		}
		else
			return false;
	}
}
###------------------------ 固定设置 ---------------------------###
/*
## 数据表名
class DbTable{
	var $admin				= "www_admin";
	
}
*/
###------------------以下参数无需设置-------------------------###
$dbconn = new DbConn($mHost,$mUser,$mPassword,$mName);
//$dbtable = new DbTable;
 ###------------------------ 结束设置 ---------------------------###
 ###mysql5.0设置数据库的字符集。。。
$dbconn->Query("SET NAMES 'utf8'");
?>